# -*- coding: utf-8 -*-
from pymongo import MongoClient
import tkinter as tk
import requests
from PIL import Image,ImageTk

header={
        'User-Agent': 'Chrome/94.0.4606.61 Safari/537.36'}


#创建窗口
root=tk.Toplevel()
#设置标题
root.title('猫眼信息获取')
#设置窗口大小
root.geometry('800x600+300+150')

#连接数据库
client=MongoClient()

database=client['maoyan1']
#创建集合
db=database['mao']

#获取全部数据
content=[x for x in db.find()]

#对数据进行处理
movie_actor=[]
movie_time=[]
movie_score=[]
movie_name=[]
movie_url=[]

for i in range(len(content)):
    movie_name.append(content[i]['电影名字'])
    movie_score.append(content[i]['电影得分'])
    movie_time.append(content[i]['上映时间'].strip('上映时间：'))
    movie_actor.append(content[i]['主演'].strip('主演：'))
    movie_url.append(content[i]['电影图片url'])

#保存符合条件的图片url
def mo(df):
    bh=[]
    bh.append(df)
    return bh

#重置图片（按要求缩放）
#def resize(w1, h1, image): 
#    w, h = image.size #获取图像的原始大小   
#    f1 = 1.0*w1/w 
#    f2 = 1.0*h1/h    
#    factor = min([f1, f2])   
#    width = int(w*factor)    
#    height = int(h*factor) 
#    #Image.ANTIALIAS：高质量
#    return image.resize((width, height), Image.ANTIALIAS)
##显示图像
#def show_image(image):
#    im=Image.open(image)                              
#    img=resize(200, 200, im)               
#    img=ImageTk.PhotoImage(img)
#    return img
    

#筛选
def call():
    po=e.get()
    so=s.get()
    bo=t.get()
    for j in range(len(movie_actor)):
        ch='电影名字:'+movie_name[j]+' '+content[j]['主演']+' '+content[j]['上映时间']+' '+'评分:'+movie_score[j]+'\n' +movie_url[j]+'\n'
        #输入主演、电影名字、评分可进行筛选
        if po in movie_actor[j] and so in movie_name[j] and bo in movie_score[j]:                                  
            text.insert(tk.INSERT,ch)
            url=mo(movie_url[j])
            for k in url:
                source=requests.get(k,headers=header)
                #设置图片下载地址
                image_path = f"F:/python练习/image/{movie_name[j]+'.jpg'}"  # 设置保存图片的名字
                with open(image_path,'wb+')as f:
                    f.write(source.content)


#设置图片展示                            
#img=show_image(image_path)                             
#tk.Label(root,image=img).grid(row=3,column=7)

# 清空Text
def clear():
    text.delete("1.0", tk.END)        
            
 
e=tk.StringVar()
s=tk.StringVar()
t=tk.StringVar()
##主演
tk.Label(root,text='电影主演:').grid(row =1, column = 0)
tk.Entry(root, textvariable =e, width=10).grid(row = 1, column = 1)

#电影名字
tk.Label(root,text='电影名字:').grid(row =1, column = 2)
tk.Entry(root, textvariable =s, width=10).grid(row =1, column =3) 

#评分
tk.Label(root,text='评分').grid(row=1,column=4)
tk.Entry(root, textvariable =t, width=10).grid(row =1, column = 5)

#url
tk.Label(root,text='图片URL',width=10).grid(row=1,column=6)
tk.Entry(root, textvariable =t, width=35).grid(row = 1, column = 7) 

text=tk.Text(root,width=90)
text.grid(row=3,column=0,columnspan=5)

tk.Button(root,text='查找',command=call).grid(row=6,column=0)
tk.Button(root,text='清空内容',command=clear).grid(row=6,column=4)

#下载图片按钮
#tk.Button(root,text='展示图片').grid(row=6,column=6)

root.mainloop()